package com.yisu.expressway.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.R;
import com.yisu.expressway.model.BannerItem;
import com.yisu.expressway.model.ScenicSpotDetailObj;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.service_district.MapActivity;
import com.yisu.expressway.ui.SlideShowView;
import com.yisu.expressway.utils.ToastUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

public class ScenicSpotDetailActivity extends BaseActivity {

    @Bind(R.id.slide_banner)
    protected SlideShowView mBannerSiler;
    @Bind(R.id.tv_view_name)
    protected TextView mTv_name;
    @Bind(R.id.tv_view_address)
    protected TextView mTv_address;
    @Bind(R.id.tv_view_des)
    protected TextView mTv_des;

    private static final String SERVICE_DISTRICT_ID = "service_district_id";
    private static final String SCENIC_SPOT_ID = "scenic_spot_id";

    private ScenicSpotDetailObj detailObj;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scenic_spot_detail);
        initData(getIntent());
    }

    public static void launch(Context context,int serviceId,int viewId){
        Intent mIntent = new Intent(context,ScenicSpotDetailActivity.class);
        mIntent.putExtra(SERVICE_DISTRICT_ID,serviceId);
        mIntent.putExtra(SCENIC_SPOT_ID,viewId);
        context.startActivity(mIntent);
    }

    private void initData(Intent intent){
        setTitle(R.string.scenic_spot_title);
        if(intent != null){
            getDetail(intent.getIntExtra(SERVICE_DISTRICT_ID,-1),intent.getIntExtra(SCENIC_SPOT_ID,-1));
        }
    }

    @OnClick(R.id.tv_view_address)
    public void showMap(){
        if(detailObj != null) {
            MapActivity.launchLocation(this, detailObj.longitude, detailObj.latitude, detailObj.mainTitle);
        }
    }

    private void getDetail(int serviceId,int viewId){
        Map<String, Object> params = new HashMap();
        params.put("serviceId", serviceId);
        params.put("viewSpotId", viewId);
        ApiClientHelper.postApi(ApiSetting.getScenicSpotDetail(), new TypeToken<ScenicSpotDetailObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<ScenicSpotDetailObj>>() {
            @Override
            public void onResponse(ApiResult<ScenicSpotDetailObj> result) {
                if (!result.success) {
                    ToastUtil.displayLongToastMsg(ScenicSpotDetailActivity.this, result.getMsg());
                    return;
                }

                detailObj = result.getResult();

                if(detailObj != null) {
                    updateView(detailObj);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToastUtil.displayShortToastMsg(ScenicSpotDetailActivity.this, volleyError.getMessage());
            }
        }, this);
    }

    private void updateView(ScenicSpotDetailObj detailObj){
        ArrayList<BannerItem> mBannerList = new ArrayList<>();

        if(detailObj.imageUrlList != null && detailObj.imageUrlList.size() > 0){
            for(int i = 0; i < detailObj.imageUrlList.size() ; i ++){
                BannerItem item = new BannerItem();
                item.imageUrl = detailObj.imageUrlList.get(i);
                mBannerList.add(item);
            }
        }
        mBannerSiler.initData(this,mBannerList);
        mTv_name.setText(detailObj.mainTitle);
        mTv_address.setText(detailObj.address);
        mTv_des.setText(detailObj.spotDesc);
    }
}
